<template>
  <div v-loading="loading" class="list">
    <el-form :inline="true" :model="page" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="page.params.username" placeholder="用户名" clearable />
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="page.params.name" placeholder="姓名" clearable />
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="page.params.nickname" placeholder="昵称" clearable />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="page.params.userEmail" placeholder="邮箱" clearable />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="page.params.sex" placeholder="活动区域" clearable filterable>
          <el-option label="男" :value="1" />
          <el-option label="女" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getByPage">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 添加按钮 -->
    <el-button type="primary" class="btn-class" size="mini" @click="openAddDialogVisible()">添加</el-button>
    <el-button type="primary" class="btn-class" size="mini" @click="resetPwd">重置密码</el-button>
    <el-button type="success" class="btn-class" size="mini" @click="exportAll">导出数据</el-button>
    <!-- 表格 -->
    <el-table :data="page.list" border style="width: 100%" @sort-change="changeSort" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column prop="username" label="账号" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="sex" label="性别" width="70" sortable="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1" type="success">男</el-tag>
          <el-tag v-else type="danger">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" />
      <el-table-column prop="header" label="封面">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.header"
            :preview-src-list="[scope.row.header]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="userEmail" label="邮箱" />
      <el-table-column prop="createdTime" label="创建时间" sortable="custom" />
      <el-table-column prop="updateTime" label="修改时间" sortable="custom" />
      <!-- 操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleEdit(scope.row.userId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.userId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="page"
      :current-page="page.currentPage"
      :page-sizes="[10,20,50,100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 添加 -->
    <el-dialog title="添加" :visible.sync="userAddDialog">
      <user-add @closeAddDialogVisible="closeAddDialogVisible" @getByPage="getByPage" />
    </el-dialog>

    <!-- 修改 -->
    <el-dialog title="添加" :visible.sync="userUpdateDialog">
      <user-update :user="user" @closeUpdateDialog="closeUpdateDialog" @getByPage="getByPage" />
    </el-dialog>
  </div>
</template>

<script>
import userApi from '@/api/myUser'
import userAdd from './user-add'
import userUpdate from './user-update'
export default {
  name: 'UserList',
  components: {
    userAdd,
    userUpdate
  },
  data() {
    return {
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        params: {}, // 参训参数对象
        list: [], // 数据
        sortColumn: '', // 排序列
        soreMethod: 'asc', // 排序方式
        loading: false // 加载
      },
      user: {

      },
      userAddDialog: false, // 添加弹窗
      userUpdateDialog: false, // 修改弹窗
      selectUsers: [] // 选择多个id
    }
  },
  created() {
    this.getByPage()
  },
  methods: {
    getByPage() {
      userApi.getByPage(this.page).then(res => {
        console.log(res.data)

        this.page = res.data
      })
    },
    changeSort(e) {
      if (e.order) {
        this.page.sortColumn = e.prop
        this.page.soreMethod = e.order
      } else {
        this.page.sortColumn = ''
        this.page.soreMethod = 'asc'
      }
    }, // 编辑
    handleEdit(id) {
      userApi.get(id).then(res => {
        this.user = res.data
        this.userUpdateDialog = true
      })
    }, // 删除
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除该用户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除接口
        userApi.deleteById(id).then(res => {
          this.$message.success(res.msg)
          // 刷新表格
          this.getByPage()
        })
      })
    },
    openAddDialogVisible() {
      this.userAddDialog = true
    }, // 关闭弹窗
    closeAddDialogVisible() {
      this.userAddDialog = false
    },
    closeUpdateDialog() {
      this.userUpdateDialog = false
    },
    handleSizeChange(val) {
      this.page.pageSize = val
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
    },
    handleSelectionChange(val) {
    //   console.log(val)

      this.selectUsers = val
    },
    resetPwd() {
      this.$confirm('是否重置该?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const userId = []
        // 循环遍历
        this.selectUsers.forEach(item => {
          console.log(item)
          userId.push(item.userId)
        })
        // 修改密码
        userApi.restPwd(userId).then(res => {
          this.$message.success(res.msg)
        })
      })
    },
    // 导出
    exportAll() {
      this.loading = true
      userApi.exportExcel().then(res => {
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
        // 创建一个A标签
        const elink = document.createElement('a')
        elink.download = '系统用户.xlsx'
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        // 追加节点
        document.body.appendChild(elink)
        elink.click()

        // 移除
        URL.revokeObjectURL(elink.href)
        document.body.removeChild(elink)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
